package com.ihteima.service.impl;

import com.ihteima.exception.BaseExceptionInterface;
import com.ihteima.exception.BizException;
import com.ihteima.exception.ResponseCodeEnum;
import com.ihteima.mapper.UserMapper;
import com.ihteima.model.vo.UpdatePasswordReqVO;
import com.ihteima.model.vo.UpdateUserInfoDO;
import com.ihteima.model.vo.UpdateUserInfoReqVO;
import com.ihteima.pojo.Response;
import com.ihteima.pojo.User;
import com.ihteima.service.UserService;
import com.ihteima.utils.JwtUtil;
import com.ihteima.utils.Md5Util;
import com.ihteima.utils.ThreadLocalUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * ClassName: UserServiceImpl
 * Package: com.ihteima.service.impl
 * Description:
 *
 * @Auther gongkaiming
 * @Create 2025/10/26 17:46
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户
     *
     * @param username
     * @return
     */
    @Override
    public User findByName(String username) {
        // 查询数据库
        return userMapper.findByName(username);
    }

    /**
     * 用户注册
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Response<?> registry(String username, String password) {
        String md5String = Md5Util.getMD5String(password);
        int count = userMapper.insert(username, md5String);
        if (count == 0) {
            throw new BizException(ResponseCodeEnum.REGISTRY_FAIL);
        }
        return Response.success();
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Response<?> login(String username, String password) {
        User userDO = userMapper.findByName(username);

        if (Objects.isNull(userDO)) {
            throw new BizException(ResponseCodeEnum.USER_NOT_EXIST);
        }
        // 判断用户用户名和密码是否正确
        if (!Md5Util.getMD5String(password).equals(userDO.getPassword())) {
            throw new BizException(ResponseCodeEnum.PASSWORD_NOT_CORRECT);
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", userDO.getId());
        String token = JwtUtil.genToken(claims);

        return Response.success(token);
    }

    /**
     * 根据用户id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public User findById(Long id) {
        return userMapper.findById(id);
    }

    /**
     * 更新用户信息
     *
     * @param updateUserInfoReqVO
     * @return
     */
    @Override
    public Response<?> updateUserInfo(UpdateUserInfoReqVO updateUserInfoReqVO) {
        Long id = updateUserInfoReqVO.getId();
        String username = updateUserInfoReqVO.getUsername();
        String avatar = updateUserInfoReqVO.getAvatar();
        String email = updateUserInfoReqVO.getEmail();
        String nickname = updateUserInfoReqVO.getNickname();

        Long currId = ThreadLocalUtil.get();

        if (!Objects.equals(id, currId)) {
            throw new BizException(ResponseCodeEnum.NOT_AUTHORIZATION);
        }

        UpdateUserInfoDO updateUserInfoDO = UpdateUserInfoDO.builder()
                .id(id)
                .username(username)
                .nickname(nickname)
                .avatar(avatar)
                .email(email)
                .updateTime(LocalDateTime.now())
                .build();


        userMapper.updateUserInfo(updateUserInfoDO);
        return Response.success();
    }

    @Override
    public Response<?> updatePassword(UpdatePasswordReqVO updatePasswordReqVO) {
        Long id = updatePasswordReqVO.getId();
        String password = updatePasswordReqVO.getPassword();

        Long currId = ThreadLocalUtil.get();

        if (!Objects.equals(id, currId)) {
            throw new BizException(ResponseCodeEnum.NOT_AUTHORIZATION);
        }
        String md5String = Md5Util.getMD5String(password);
        int count = userMapper.updatePassword(id, md5String);
        
        if (0 == count) {
            throw new BizException(ResponseCodeEnum.UPDATE_FAIL);
        }

        return Response.success();
    }
}
